/***************************************************************************
+ * Copyright 2012-2013 TXT e-solutions SpA
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the contract ICT-257367.
 *
 * Authors:
 *      Salvatore Piccione (TXT e-solutions SpA)
 *
 * Contributors:
 *        Domenico Rotondi (TXT e-solutions SpA)
 **************************************************************************/
package it.txt.ens.core.impl;

import it.txt.ens.core.X509CertificateRetrievalParameters;
import it.txt.ens.core.factory.X509CertificateRetrievalParametersFactory;

/**
 * Provides the default implementation of <code>X509CertificateRetrievalParameters</code>
 * 
 * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT txtgroup.com)
 * @author Domenico Rotondi (TXT e-solutions SpA - domenico.rotondi AT txtgroup.com)
 *
 */
public class BasicX509CertificateRetrievalParameters 
implements X509CertificateRetrievalParameters{
    
    private final char[] privateKeyPassword;
    private final String x509CertificateSubject;
    
    private BasicX509CertificateRetrievalParameters (char[] privateKeyPassword,
            String x509CertificateSubject) {
        if (privateKeyPassword == null)
            throw new IllegalArgumentException("The private key password cannot be null");
        if (privateKeyPassword.length < 1)
            throw new IllegalArgumentException("The private key password cannot be null");
        if (x509CertificateSubject == null)
            throw new IllegalArgumentException("The X.509 subject cannot be null");
        if (x509CertificateSubject.length() == 0)
            throw new IllegalArgumentException("The X.509 subject cannot be an empty string");
        
        this.privateKeyPassword = privateKeyPassword;
        this.x509CertificateSubject = x509CertificateSubject;
    }
    
    /* (non-Javadoc)
     * @see it.txt.ens.core.X509CertificateRetrievalParameters#getPrivateKeyPassword()
     */
    @Override
    public char[] getPrivateKeyPassword() {
        return privateKeyPassword;
    }

    /* (non-Javadoc)
     * @see it.txt.ens.core.X509CertificateRetrievalParameters#getX509CertificateSubject()
     */
    @Override
    public String getX509CertificateSubject() {
        return x509CertificateSubject;
    }
    
    public static class BasicX509CertificateRetrievalParametersFactory implements 
    X509CertificateRetrievalParametersFactory {
        
        /* (non-Javadoc)
         * @see it.txt.ens.core.factory.X509CertificateRetrievalParametersFactory#create(char[], java.lang.String)
         */
        @Override
        public X509CertificateRetrievalParameters create(String x509CertificateSubject,
                char[] privateKeyPassword)
                throws IllegalArgumentException {
            return new  BasicX509CertificateRetrievalParameters(privateKeyPassword, x509CertificateSubject);
        }
        
    }
}
